import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import './icons' // icon
import './errorLog' // error log
import './permission' // permission control
// import './mock' // simulation data

import * as filters from './filters' // global filters
import PermissionDirective from './directive/permission/index'
import FocusDirective from './directive/focus/index'
import { getSequence } from '@/utils'
import SmallImage from '@/components/SmallImage'

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})

// 自定义组件
Vue.component('small-image', SmallImage)

// register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
Vue.use(PermissionDirective)
Vue.use(FocusDirective)

Vue.config.productionTip = false
Vue.config.silent = true

// 全局方法
Vue.mixin({
  data() {
    return {
      $vm: {
        vmId: '',
        opener: '',
        callback: ''
      },
      appCommon: {
        pageSize: [10, 20, 30, 50, 100, 500]
      }
    }
  },
  mounted() {
    this.$data.$vm.vmId = getSequence('route')
    if (window.$vms === undefined) {
      window.$vms = {}
    }
    window.$vms[this.$data.$vm.vmId] = this
    if (this.$route && this.$route.query) {
      this.$data.$vm.opener = this.$route.query.opener
      this.$data.$vm.callback = this.$route.query.callback
    }
  },
  beforeDestroy() {
    window.$vms[this.$data.$vm.vmId] = undefined
  },
  methods: {
    closeCurrentTag() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    // 新建页面(tab类型的)
    callNewPage(path, callback) {
      const cPath = path + '?' + 'opener=' + this.$data.$vm.vmId + '&callback=' + callback
      this.$router.push({ path: cPath })
    },
    // 页面执行完毕，返回上一个页面 ，如果有传入callback,则执行调入页的回调函数
    pageOver() {
      if (this.$data.$vm.callback !== undefined && this.$data.$vm.callback !== null && this.$data.$vm.callback !== 'undefined') {
        if (window.$vms[this.$data.$vm.opener] !== undefined) {
          window.$vms[this.$data.$vm.opener][this.$data.$vm.callback](...arguments)
        }
      }
      this.closeCurrentTag()
    }
  }
})

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
